package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Now()
	num := 1000000 //  依次增加到100万个数组成员
	for i := 1; i <= 10; i++ {
		ArrayListExtend(num)
	}
	fmt.Printf("单次平均耗时：%d ms\n", time.Now().Sub(start).Milliseconds()/10)

}

// 动态数组的扩容与内存拷贝性能测试
func ArrayListExtend(arrNum int) {

	var arrList []int
	var extendCount = 0
	var tmpCapacity = cap(arrList)
	for i := 1; i <= arrNum+1; i++ {
		arrList = append(arrList, i)
		if tmpCapacity != cap(arrList) {
			extendCount++
			fmt.Printf("成员个数：%d, 数组出现了扩容：%d ————>%d \n", len(arrList), tmpCapacity, cap(arrList))
			tmpCapacity = cap(arrList)
		}
	}

	fmt.Printf("累计添加了 %d 个数组元素，扩容次数: %d ,最后一个元素：%d\n", arrNum, extendCount, arrList[arrNum-1])

}
